package com.acc.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.sql.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.acc.entity.EasyUIDataGridJSON;
import com.acc.entity.Msg;
import com.acc.entity.RoleName;
import com.acc.entity.Standby;
import com.acc.entity.User;
import com.acc.service.AdminUserService;
import com.acc.service.AuthorityService;
import com.acc.service.RoleService;
import com.acc.service.RoleUriService;
import com.acc.service.StandbyService;
import com.acc.service.impl.AdminUserServiceImpl;
import com.acc.service.impl.AuthorityServiceImpl;
import com.acc.service.impl.RoleServiceImpl;
import com.acc.service.impl.RoleUriServiceImpl;
import com.acc.service.impl.StandbyServiceImpl;
import com.alibaba.fastjson.JSON;
/**
 * 
 * @author 朱春阳
 *
 */
public class TicketController extends InnerDispacherController2 {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public void pageSelect(HttpServletRequest request, HttpServletResponse response) {
		StandbyService standbyservice = new StandbyServiceImpl();
		Integer page = null;
		Integer rows = null;
		// 获取分页数据
		try {
			page = Integer.parseInt(request.getParameter("page"));
			rows = Integer.parseInt(request.getParameter("rows"));
		} catch (NumberFormatException nfe) {
			nfe.printStackTrace();
		}
		// 获取总数目
		Integer count = standbyservice.getAllTicketSize();
		List<Standby> pageusers = standbyservice.getAllTickets(page, rows);

		EasyUIDataGridJSON easyUIDataGridJSON = new EasyUIDataGridJSON();
		easyUIDataGridJSON.setTotal(count);
		easyUIDataGridJSON.setRows(pageusers);

		PrintWriter pw = null;
		try {
			pw = response.getWriter();
			response.setContentType("application/json");
			// 转换成json格式
			pw.append(JSON.toJSONString(easyUIDataGridJSON));

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			pw.close();
		}

	}

	public void addTicket(HttpServletRequest req, HttpServletResponse resp) throws ParseException {
		StandbyService standbyService = new StandbyServiceImpl();

		// 从输入框中取出值
		String startLocation = req.getParameter("startLocation");
		String endLocation = req.getParameter("endLocation");
		String startTime = req.getParameter("startTime");
		String endTime = req.getParameter("endTime");

		String startDay = req.getParameter("startDay");
		Integer num = null;
		Integer type = null;
		Integer price = null;
		try {
			num = Integer.parseInt(req.getParameter("num"));
			price = Integer.parseInt(req.getParameter("price"));
			type = Integer.parseInt(req.getParameter("type"));
		} catch (NumberFormatException nfe) {
			nfe.printStackTrace();
		}

		// 格式化日期
		SimpleDateFormat format = new SimpleDateFormat("yyyy/mm/dd hh:mm:ss");
		SimpleDateFormat format1 = new SimpleDateFormat("yyyy/mm/dd");
		java.util.Date d = null; // startTime
		java.util.Date d1 = null; // endTime
		java.util.Date start_day = null;
		try {
			d = format.parse(startTime);
			d1 = format.parse(endTime);
			start_day = format1.parse(startDay);
		} catch (Exception e) {
			e.printStackTrace();
		}
		java.sql.Date start_date = new java.sql.Date(d.getTime());
		Date end_date = new Date(d1.getTime());
		Date start_d = new Date(start_day.getTime());

		// 将取出的值封装起来
		Standby standby = new Standby();
		standby.setEndLocation(endLocation);
		standby.setEndTime(end_date);
		standby.setNum(num);
		standby.setPrice(price);
		standby.setStartDay(start_d);
		standby.setStartLocation(startLocation);
		standby.setStartTime(start_date);
		standby.setType(type);
		// 增加记录
		boolean flag = standbyService.addStandby(standby);

		// 存储判断的信息
		Msg msg = new Msg();
		String result = null;
		if (flag) {
			msg.setErrorMsg(null);
			msg.setSuccess(true);
			result = JSON.toJSONString(msg);
		} else {
			msg.setErrorMsg("增加失败");
			msg.setSuccess(false);
			result = JSON.toJSONString(msg);
		}

		try {
			resp.getWriter().append(result);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void editTicket(HttpServletRequest req, HttpServletResponse resp) {
		StandbyService standbyService = new StandbyServiceImpl();

		// 从输入框中取出值
		String startLocation = req.getParameter("startLocation");
		String endLocation = req.getParameter("endLocation");
		String startTime = req.getParameter("startTime");
		String endTime = req.getParameter("endTime");

		String startDay = req.getParameter("startDay");
		Integer num = null;
		Integer type = null;
		Integer price = null;
		Integer id = null;
		try {
			id = Integer.parseInt(req.getParameter("id"));
			num = Integer.parseInt(req.getParameter("num"));
			price = Integer.parseInt(req.getParameter("price"));
			type = Integer.parseInt(req.getParameter("type"));
		} catch (NumberFormatException nfe) {
			nfe.printStackTrace();
		}

		// 格式化日期
		SimpleDateFormat format = new SimpleDateFormat("yyyy/mm/dd hh:mm:ss");
		SimpleDateFormat format1 = new SimpleDateFormat("yyyy/mm/dd");
		java.util.Date d = null; // startTime
		java.util.Date d1 = null; // endTime
		java.util.Date start_day = null;
		try {
			d = format.parse(startTime);
			d1 = format.parse(endTime);
			start_day = format1.parse(startDay);
		} catch (Exception e) {
			e.printStackTrace();
		}
		java.sql.Date start_date = new java.sql.Date(d.getTime());
		Date end_date = new Date(d1.getTime());
		Date start_d = new Date(start_day.getTime());

		// 将取出的值封装起来
		Standby standby = new Standby();
		standby.setEndLocation(endLocation);
		standby.setEndTime(end_date);
		standby.setNum(num);
		standby.setPrice(price);
		standby.setStartDay(start_d);
		standby.setStartLocation(startLocation);
		standby.setStartTime(start_date);
		standby.setType(type);
		standby.setId(id);

		boolean flag = standbyService.updateStandby(standby);
		System.out.println(flag);
		// 存储判断的信息
		Msg msg = new Msg();
		String result = null;
		if (flag) {
			msg.setErrorMsg(null);
			msg.setSuccess(true);
			result = JSON.toJSONString(msg);
		} else {
			msg.setErrorMsg("删除失败");
			msg.setSuccess(false);
			result = JSON.toJSONString(msg);
		}

		
		try {
			resp.setContentType("application/json");
			resp.getWriter().append(result);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void deleteTicket(HttpServletRequest request, HttpServletResponse response) {
		// 从页面获取id
		String id_str = request.getParameter("id");
		Integer id = null;
		try {
			id = Integer.parseInt(id_str);
		} catch (NumberFormatException nfe) {
			nfe.printStackTrace();
		}

		StandbyService standbyService = new StandbyServiceImpl();

		// 判断删除是否成功
		boolean flag = standbyService.deleteOnRecorde(id);

		// 存储判断的信息
		Msg msg = new Msg();
		String result = null;
		if (flag) {
			msg.setErrorMsg(null);
			msg.setSuccess(true);
			result = JSON.toJSONString(msg);
		} else {
			msg.setErrorMsg("删除失败");
			msg.setSuccess(false);
			result = JSON.toJSONString(msg);
		}

		response.setContentType("application/json");
		try {
			response.getWriter().append(result);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
